package com.foreveross.crawl.application.impl.mq;

import java.util.List;

import javax.annotation.Resource;

import net.sf.json.JSONObject;

import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Service;

import com.foreveross.crawl.application.mq.IMessageOperateForMQ;
import com.foreveross.crawl.vo.Message;
import com.foreveross.mq.CacheDataSpace;
import com.foreveross.mq.DestinationFactory;

//@Service
public class MessageOperateForMQImpl implements IMessageOperateForMQ {

//	@Resource(name = "sendJmsTemplate")
	private JmsTemplate jmsTemplate;
	
	@Override
	public void sendMessages(List<Message> messageList) throws Exception {

		for(Message message : messageList){
			this.sendMessage(message);
		}
	}

	@Override
	public void sendMessage(Message message) throws Exception {

		jmsTemplate.convertAndSend(DestinationFactory.getInstance().getCommandDestination(
				new CacheDataSpace(message.getCommand()), message.getCommand()), message);
	}

	@Override
	public void sendMessage(String message) throws Exception {
		Message mes = (Message) JSONObject.toBean(JSONObject.fromObject(message), Message.class);
		jmsTemplate.convertAndSend(DestinationFactory.getInstance().getCommandDestination(
				new CacheDataSpace(mes.getCommand())), message);
	}

}
